import {
  FieldDataSourceTypeEnum,
  FieldRuleEnum,
  FieldTypeEnum,
  FormDesignKeyEnum,
} from '@lib/shared/enums/formDesignEnum';
import type { CommonList } from '@lib/shared/models/common';
import type { FormDesignConfigDetailParams } from '@lib/shared/models/system/module';

import {
  addClue,
  addClueFollowPlan,
  addClueFollowRecord,
  addContract,
  addCustomer,
  addCustomerContact,
  addCustomerFollowPlan,
  addCustomerFollowRecord,
  addOpportunity,
  addOptFollowPlan,
  addOptFollowRecord,
  addPaymentPlan,
  addProduct,
  addProductPrice,
  addQuotation,
  advancedSearchOptPage,
  ClueTransitionCustomer,
  geAdvancedCustomerList,
  getAdvancedCluePoolList,
  getAdvancedCustomerContactList,
  getAdvancedOpenSeaCustomerList,
  getAdvancedSearchClueList,
  getClue,
  getClueFollowPlan,
  getClueFollowRecord,
  getClueFormConfig,
  getClueList,
  getCluePoolList,
  getClueTransitionCustomerList,
  getContactListUnderCustomer,
  getContractDetail,
  getContractFormConfig,
  getContractFormSnapshotConfig,
  getContractList,
  getContractPaymentPlanList,
  getCustomer,
  getCustomerContact,
  getCustomerContactFormConfig,
  getCustomerContactList,
  getCustomerFollowPlan,
  getCustomerFollowPlanFormConfig,
  getCustomerFollowRecord,
  getCustomerFollowRecordFormConfig,
  getCustomerFormConfig,
  getCustomerList,
  getCustomerOpportunityPage,
  getFollowPlanDetail,
  getFollowPLanPage,
  getFollowRecordDetail,
  getFollowRecordPage,
  getOpenSeaCustomer,
  getOpenSeaCustomerList,
  getOpportunityContactList,
  getOpportunityDetail,
  getOpportunityList,
  getOptFollowPlan,
  getOptFollowRecord,
  getOptFormConfig,
  getPaymentPlanDetail,
  getPaymentPlanFormConfig,
  getPaymentPlanList,
  getPoolClue,
  getProduct,
  getProductFormConfig,
  getProductList,
  getProductPrice,
  getProductPriceFormConfig,
  getProductPriceList,
  getQuotationDetail,
  getQuotationFormConfig,
  getQuotationList,
  getQuotationSnapshotFormConfig,
  updateClue,
  updateClueFollowPlan,
  updateClueFollowRecord,
  updateContract,
  updateCustomer,
  updateCustomerContact,
  updateCustomerFollowPlan,
  updateCustomerFollowRecord,
  updateFollowPlan,
  updateFollowRecord,
  updateOpportunity,
  updateOptFollowPlan,
  updateOptFollowRecord,
  updatePaymentPlan,
  updateProduct,
  updateProductPrice,
  updateQuotation,
} from '@/api/modules';

import type { FormCreateField, FormCreateFieldRule, FormDetail } from './types';

export const multipleValueTypeList = [
  FieldTypeEnum.MEMBER_MULTIPLE,
  FieldTypeEnum.DEPARTMENT_MULTIPLE,
  FieldTypeEnum.DATA_SOURCE_MULTIPLE,
  FieldTypeEnum.CHECKBOX,
  FieldTypeEnum.SELECT_MULTIPLE,
  FieldTypeEnum.INPUT_MULTIPLE,
  FieldTypeEnum.ATTACHMENT,
];

export const inputDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.INPUT,
  name: 'crmFormDesign.input',
  icon: 'iconicon_single_line_text',
  fieldWidth: 1,
  showLabel: true,
  defaultValue: '',
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
};

export const textareaDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.TEXTAREA,
  icon: 'iconicon_multiline',
  name: 'crmFormDesign.textarea',
  fieldWidth: 1,
  showLabel: true,
  defaultValue: '',
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
};

export const inputNumberDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.INPUT_NUMBER,
  icon: 'iconicon_hashtag_key',
  name: 'crmFormDesign.inputNumber',
  fieldWidth: 1,
  showLabel: true,
  defaultValue: null,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  numberFormat: 'number',
  decimalPlaces: false,
  precision: 0,
  showThousandsSeparator: false,
};

export const dateTimeDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.DATE_TIME,
  icon: 'iconicon_calendar1',
  name: 'crmFormDesign.dateTime',
  fieldWidth: 1,
  showLabel: true,
  defaultValue: null,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  dateType: 'datetime',
  dateDefaultType: 'custom', // 自定义日期类型
};

export const radioDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.RADIO,
  icon: 'iconicon_radio',
  name: 'crmFormDesign.radio',
  fieldWidth: 1,
  showLabel: true,
  defaultValue: '',
  description: '',
  options: [],
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  direction: 'vertical',
};

export const checkboxDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.CHECKBOX,
  icon: 'icona-icon_collectbeifen12',
  name: 'crmFormDesign.checkbox',
  fieldWidth: 1,
  showLabel: true,
  defaultValue: [],
  description: '',
  options: [],
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  direction: 'vertical',
};

export const selectDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.SELECT,
  icon: 'iconicon_pull_down_single_choice',
  name: 'crmFormDesign.select',
  fieldWidth: 1,
  showLabel: true,
  options: [],
  defaultValue: '',
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  multiple: false,
};

export const selectMultipleDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.SELECT_MULTIPLE,
  icon: 'iconicon_pull_down_single_choice',
  name: 'crmFormDesign.selectMultiple',
  fieldWidth: 1,
  showLabel: true,
  options: [],
  defaultValue: [],
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  multiple: false,
};

export const memberDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.MEMBER,
  icon: 'iconicon_member_single_choice',
  name: 'crmFormDesign.memberSelect',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  defaultValue: [],
  initialOptions: [],
  multiple: false,
};

export const memberMultipleDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.MEMBER_MULTIPLE,
  icon: 'iconicon_member_single_choice',
  name: 'crmFormDesign.memberSelectMultiple',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  defaultValue: [],
  initialOptions: [],
  multiple: false,
};

export const departmentDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.DEPARTMENT,
  icon: 'iconicon_department_single_choice',
  name: 'crmFormDesign.departmentSelect',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  defaultValue: [],
  initialOptions: [],
  multiple: false,
};

export const departmentMultipleDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.DEPARTMENT_MULTIPLE,
  icon: 'iconicon_department_single_choice',
  name: 'crmFormDesign.departmentSelectMultiple',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  defaultValue: [],
  initialOptions: [],
  multiple: false,
};

export const dividerDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.DIVIDER,
  icon: 'iconicon_dividing_line',
  name: 'crmFormDesign.divider',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  dividerClass: 'divider--normal',
  dividerColor: '#edf0f1',
  titleColor: '#323535',
};

export const tagInputDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.INPUT_MULTIPLE,
  icon: 'iconicon_books',
  name: 'common.tag',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  defaultValue: [],
};

export const basicFields: FormCreateField[] = [
  inputDefaultFieldConfig,
  textareaDefaultFieldConfig,
  inputNumberDefaultFieldConfig,
  dateTimeDefaultFieldConfig,
  radioDefaultFieldConfig,
  checkboxDefaultFieldConfig,
  selectDefaultFieldConfig,
  selectMultipleDefaultFieldConfig,
  memberDefaultFieldConfig,
  memberMultipleDefaultFieldConfig,
  departmentDefaultFieldConfig,
  departmentMultipleDefaultFieldConfig,
  dividerDefaultFieldConfig,
  tagInputDefaultFieldConfig,
];

export const pictureDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.PICTURE,
  icon: 'iconicon_picture',
  name: 'crmFormDesign.picture',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  defaultValue: [],
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  uploadLimit: 10,
  uploadSizeLimit: 20,
  pictureShowType: 'card',
  uploadLimitEnable: false,
  uploadSizeLimitEnable: false,
};

export const fileDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.ATTACHMENT,
  icon: 'iconicon_attach',
  name: 'crmFormDesign.file',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  defaultValue: [],
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  onlyOne: false,
  accept: '',
  limitSize: '',
};

export const industryDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.INDUSTRY,
  icon: 'iconicon_industry',
  name: 'crmFormDesign.industry',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  defaultValue: [],
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
};

export const locationDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.LOCATION,
  icon: 'iconicon_map',
  name: 'crmFormDesign.location',
  fieldWidth: 1,
  showLabel: true,
  defaultValue: '',
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  locationType: 'PCD',
};

export const phoneDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.PHONE,
  icon: 'iconicon_phone',
  name: 'crmFormDesign.phone',
  fieldWidth: 1,
  format: '255',
  showLabel: true,
  description: '',
  defaultValue: null,
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
};

export const dataSourceDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.DATA_SOURCE,
  icon: 'iconicon_select_data',
  name: 'crmFormDesign.dataSourceSingle',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  multiple: false,
  defaultValue: [],
  initialOptions: [],
  dataSourceType: FieldDataSourceTypeEnum.CUSTOMER,
  combineSearch: {
    searchMode: 'OR', // 默认搜索模式
    conditions: [
      {
        leftFieldId: undefined,
        leftFieldType: FieldTypeEnum.INPUT,
        operator: undefined,
        rightFieldId: undefined,
        rightFieldCustom: false,
        rightFieldCustomValue: '',
        rightFieldType: FieldTypeEnum.INPUT, // 默认右侧字段类型为输入框
      },
    ],
  },
};

export const dataSourceMultipleDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.DATA_SOURCE_MULTIPLE,
  icon: 'iconicon_select_data',
  name: 'crmFormDesign.dataSourceMultiple',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  multiple: false,
  defaultValue: [],
  initialOptions: [],
  dataSourceType: FieldDataSourceTypeEnum.CUSTOMER,
  combineSearch: {
    searchMode: 'OR', // 默认搜索模式
    conditions: [
      {
        leftFieldId: undefined,
        leftFieldType: FieldTypeEnum.INPUT,
        operator: undefined,
        rightFieldId: undefined,
        rightFieldCustom: false,
        rightFieldCustomValue: '',
        rightFieldType: FieldTypeEnum.INPUT, // 默认右侧字段类型为输入框
      },
    ],
  },
};

export const serialNumberDefaultFieldConfig: FormCreateField = {
  id: '',
  defaultValue: '',
  type: FieldTypeEnum.SERIAL_NUMBER,
  icon: 'iconicon_serial_number',
  name: 'crmFormDesign.serialNumber',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: false,
  mobile: true,
  rules: [],
  serialNumberRules: ['Opp', '-', 'yyyyMM', '-', 6],
};

export const linkDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.LINK,
  icon: 'iconicon_link2',
  name: 'system.message.href',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  openMode: 'openInNew',
  linkSource: 'userInput',
  rules: [],
};
export const formulaDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.FORMULA,
  icon: 'iconicon_arithmetic',
  name: 'crmFormDesign.calculation',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: false,
  mobile: true,
  rules: [],
  formula: '',
};

export const productTableDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.SUB_PRODUCT,
  icon: 'iconicon_table',
  name: 'crmFormDesign.productTable',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  subFields: [],
  fixedColumn: 1,
};

export const priceTableDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.SUB_PRICE,
  icon: 'iconicon_table',
  name: 'crmFormDesign.priceTable',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  subFields: [],
  fixedColumn: 1,
};

export const advancedFields: FormCreateField[] = [
  pictureDefaultFieldConfig,
  locationDefaultFieldConfig,
  phoneDefaultFieldConfig,
  dataSourceDefaultFieldConfig,
  dataSourceMultipleDefaultFieldConfig,
  serialNumberDefaultFieldConfig,
  linkDefaultFieldConfig,
  fileDefaultFieldConfig,
  industryDefaultFieldConfig,
  formulaDefaultFieldConfig,
  productTableDefaultFieldConfig,
  priceTableDefaultFieldConfig,
];

export const rules: FormCreateFieldRule[] = [
  {
    key: FieldRuleEnum.REQUIRED,
    required: true,
    message: 'common.notNull',
    label: 'common.required',
    trigger: ['change', 'blur'],
  },
  {
    key: FieldRuleEnum.UNIQUE,
    trigger: 'blur',
    label: 'common.onlyOne',
    validator: () => true, // 由后台验证
  },
];

export const showRulesMap: Record<FieldTypeEnum, FieldRuleEnum[]> = {
  [FieldTypeEnum.INPUT]: [FieldRuleEnum.REQUIRED, FieldRuleEnum.UNIQUE],
  [FieldTypeEnum.TEXTAREA]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.INPUT_NUMBER]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.DATE_TIME]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.RADIO]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.CHECKBOX]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.SELECT]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.SELECT_MULTIPLE]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.MEMBER]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.MEMBER_MULTIPLE]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.DEPARTMENT]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.DEPARTMENT_MULTIPLE]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.DIVIDER]: [],
  [FieldTypeEnum.USER_TAG_SELECTOR]: [],
  [FieldTypeEnum.USER_SELECT]: [],
  [FieldTypeEnum.TIME_RANGE_PICKER]: [],
  [FieldTypeEnum.TREE_SELECT]: [],
  [FieldTypeEnum.INPUT_MULTIPLE]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.PICTURE]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.LOCATION]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.PHONE]: [FieldRuleEnum.REQUIRED, FieldRuleEnum.UNIQUE],
  [FieldTypeEnum.DATA_SOURCE]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.DATA_SOURCE_MULTIPLE]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.SERIAL_NUMBER]: [],
  [FieldTypeEnum.LINK]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.ATTACHMENT]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.INDUSTRY]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.FORMULA]: [],
  [FieldTypeEnum.SUB_PRODUCT]: [],
  [FieldTypeEnum.SUB_PRICE]: [],
};

export const getFormConfigApiMap: Record<FormDesignKeyEnum, (id?: string) => Promise<FormDesignConfigDetailParams>> = {
  [FormDesignKeyEnum.CUSTOMER]: getCustomerFormConfig,
  [FormDesignKeyEnum.BUSINESS]: getOptFormConfig,
  [FormDesignKeyEnum.CONTACT]: getCustomerContactFormConfig,
  [FormDesignKeyEnum.CUSTOMER_CONTACT]: getCustomerContactFormConfig,
  [FormDesignKeyEnum.FOLLOW_PLAN_CUSTOMER]: getCustomerFollowPlanFormConfig,
  [FormDesignKeyEnum.FOLLOW_RECORD_CUSTOMER]: getCustomerFollowRecordFormConfig,
  [FormDesignKeyEnum.FOLLOW_PLAN_CLUE]: getCustomerFollowPlanFormConfig,
  [FormDesignKeyEnum.FOLLOW_RECORD_CLUE]: getCustomerFollowRecordFormConfig,
  [FormDesignKeyEnum.FOLLOW_PLAN_BUSINESS]: getCustomerFollowPlanFormConfig,
  [FormDesignKeyEnum.FOLLOW_RECORD_BUSINESS]: getCustomerFollowRecordFormConfig,
  [FormDesignKeyEnum.CLUE]: getClueFormConfig,
  [FormDesignKeyEnum.CLUE_TRANSITION_CUSTOMER]: getCustomerFormConfig,
  [FormDesignKeyEnum.CLUE_POOL]: getClueFormConfig,
  [FormDesignKeyEnum.PRODUCT]: getProductFormConfig,
  [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: getCustomerFormConfig,
  [FormDesignKeyEnum.BUSINESS_CONTACT]: getCustomerContactFormConfig,
  [FormDesignKeyEnum.CUSTOMER_OPPORTUNITY]: getOptFormConfig,
  [FormDesignKeyEnum.FOLLOW_RECORD]: getCustomerFollowRecordFormConfig,
  [FormDesignKeyEnum.FOLLOW_PLAN]: getCustomerFollowPlanFormConfig,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE]: getClueFormConfig,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CUSTOMER]: getCustomerFormConfig,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CONTACT]: getCustomerContactFormConfig,
  [FormDesignKeyEnum.SEARCH_ADVANCED_PUBLIC]: getCustomerFormConfig,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE_POOL]: getClueFormConfig,
  [FormDesignKeyEnum.SEARCH_ADVANCED_OPPORTUNITY]: getOptFormConfig,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: getQuotationFormConfig,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION_SNAPSHOT]: (id) => getQuotationSnapshotFormConfig(id),
  [FormDesignKeyEnum.CONTRACT_SNAPSHOT]: (id) => getContractFormSnapshotConfig(id),
  [FormDesignKeyEnum.CONTRACT]: getContractFormConfig,
  [FormDesignKeyEnum.CONTRACT_PAYMENT]: getPaymentPlanFormConfig,
  [FormDesignKeyEnum.CONTRACT_CONTRACT_PAYMENT]: getPaymentPlanFormConfig,
  [FormDesignKeyEnum.PRICE]: getProductPriceFormConfig,
};

export const createFormApi: Record<FormDesignKeyEnum, (data: any) => Promise<any>> = {
  [FormDesignKeyEnum.CUSTOMER]: addCustomer,
  [FormDesignKeyEnum.BUSINESS]: addOpportunity,
  [FormDesignKeyEnum.CONTACT]: addCustomerContact,
  [FormDesignKeyEnum.CUSTOMER_CONTACT]: addCustomerContact,
  [FormDesignKeyEnum.BUSINESS_CONTACT]: addCustomerContact,
  [FormDesignKeyEnum.FOLLOW_PLAN_CUSTOMER]: addCustomerFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_CUSTOMER]: addCustomerFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN_CLUE]: addClueFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_CLUE]: addClueFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN_BUSINESS]: addOptFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_BUSINESS]: addOptFollowRecord,
  [FormDesignKeyEnum.FOLLOW_RECORD]: async () => ({}),
  [FormDesignKeyEnum.FOLLOW_PLAN]: async () => ({}),
  [FormDesignKeyEnum.CLUE]: addClue,
  [FormDesignKeyEnum.CLUE_TRANSITION_CUSTOMER]: ClueTransitionCustomer,
  [FormDesignKeyEnum.CLUE_POOL]: async () => ({}),
  [FormDesignKeyEnum.PRODUCT]: addProduct,
  [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: async () => ({}), // 公海无添加
  [FormDesignKeyEnum.CUSTOMER_OPPORTUNITY]: addOpportunity,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE]: addClue,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CUSTOMER]: addCustomer,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CONTACT]: addCustomerContact,
  [FormDesignKeyEnum.SEARCH_ADVANCED_PUBLIC]: async () => ({}),
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE_POOL]: async () => ({}),
  [FormDesignKeyEnum.SEARCH_ADVANCED_OPPORTUNITY]: addOpportunity,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: addQuotation,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION_SNAPSHOT]: addQuotation,
  [FormDesignKeyEnum.CONTRACT]: addContract,
  [FormDesignKeyEnum.CONTRACT_SNAPSHOT]: addContract,
  [FormDesignKeyEnum.CONTRACT_PAYMENT]: addPaymentPlan,
  [FormDesignKeyEnum.CONTRACT_CONTRACT_PAYMENT]: addPaymentPlan,
  [FormDesignKeyEnum.PRICE]: addProductPrice,
};

export const updateFormApi: Record<FormDesignKeyEnum, (data: any) => Promise<any>> = {
  [FormDesignKeyEnum.CUSTOMER]: updateCustomer,
  [FormDesignKeyEnum.BUSINESS]: updateOpportunity,
  [FormDesignKeyEnum.CONTACT]: updateCustomerContact,
  [FormDesignKeyEnum.CUSTOMER_CONTACT]: updateCustomerContact,
  [FormDesignKeyEnum.BUSINESS_CONTACT]: updateCustomerContact,
  [FormDesignKeyEnum.FOLLOW_PLAN_CUSTOMER]: updateCustomerFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_CUSTOMER]: updateCustomerFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN_CLUE]: updateClueFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_CLUE]: updateClueFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN_BUSINESS]: updateOptFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_BUSINESS]: updateOptFollowRecord,
  [FormDesignKeyEnum.CLUE]: updateClue,
  [FormDesignKeyEnum.CLUE_TRANSITION_CUSTOMER]: async () => ({}),
  [FormDesignKeyEnum.CLUE_POOL]: async () => ({}),
  [FormDesignKeyEnum.PRODUCT]: updateProduct,
  [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: async () => ({}), // 公海无更新
  [FormDesignKeyEnum.CUSTOMER_OPPORTUNITY]: updateOpportunity,
  [FormDesignKeyEnum.FOLLOW_RECORD]: updateFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN]: updateFollowPlan,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE]: updateClue,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CUSTOMER]: updateCustomer,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CONTACT]: updateCustomerContact,
  [FormDesignKeyEnum.SEARCH_ADVANCED_PUBLIC]: async () => ({}),
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE_POOL]: async () => ({}),
  [FormDesignKeyEnum.SEARCH_ADVANCED_OPPORTUNITY]: updateOpportunity,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: updateQuotation,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION_SNAPSHOT]: updateQuotation,
  [FormDesignKeyEnum.CONTRACT]: updateContract,
  [FormDesignKeyEnum.CONTRACT_SNAPSHOT]: updateContract,
  [FormDesignKeyEnum.CONTRACT_PAYMENT]: updatePaymentPlan,
  [FormDesignKeyEnum.CONTRACT_CONTRACT_PAYMENT]: updatePaymentPlan,
  [FormDesignKeyEnum.PRICE]: updateProductPrice,
};

export const getFormDetailApiMap: Partial<Record<FormDesignKeyEnum, (id: string) => Promise<FormDetail>>> = {
  [FormDesignKeyEnum.CUSTOMER]: getCustomer,
  [FormDesignKeyEnum.BUSINESS]: getOpportunityDetail,
  [FormDesignKeyEnum.CONTACT]: getCustomerContact,
  [FormDesignKeyEnum.CUSTOMER_CONTACT]: getCustomerContact,
  [FormDesignKeyEnum.FOLLOW_PLAN_CUSTOMER]: getCustomerFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_CUSTOMER]: getCustomerFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN_CLUE]: getClueFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_CLUE]: getClueFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN_BUSINESS]: getOptFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_BUSINESS]: getOptFollowRecord,
  [FormDesignKeyEnum.CLUE]: getClue,
  [FormDesignKeyEnum.CLUE_POOL]: getPoolClue,
  [FormDesignKeyEnum.PRODUCT]: getProduct,
  [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: getOpenSeaCustomer,
  [FormDesignKeyEnum.CUSTOMER_OPPORTUNITY]: getOpportunityDetail,
  [FormDesignKeyEnum.FOLLOW_RECORD]: getFollowRecordDetail,
  [FormDesignKeyEnum.FOLLOW_PLAN]: getFollowPlanDetail,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE]: getClue,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CUSTOMER]: getCustomer,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CONTACT]: getCustomerContact,
  [FormDesignKeyEnum.SEARCH_ADVANCED_PUBLIC]: getOpenSeaCustomer,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE_POOL]: getPoolClue,
  [FormDesignKeyEnum.SEARCH_ADVANCED_OPPORTUNITY]: getOpportunityDetail,
  [FormDesignKeyEnum.CONTRACT]: getContractDetail,
  [FormDesignKeyEnum.CONTRACT_SNAPSHOT]: getContractDetail,
  [FormDesignKeyEnum.CONTRACT_PAYMENT]: getPaymentPlanDetail,
  [FormDesignKeyEnum.CONTRACT_CONTRACT_PAYMENT]: getPaymentPlanDetail,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: getQuotationDetail,
  [FormDesignKeyEnum.PRICE]: getProductPrice,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION_SNAPSHOT]: getQuotationDetail,
};

export const getFormListApiMap: Partial<Record<FormDesignKeyEnum, (data: any) => Promise<CommonList<any>>>> = {
  [FormDesignKeyEnum.CUSTOMER]: getCustomerList,
  [FormDesignKeyEnum.BUSINESS]: getOpportunityList,
  [FormDesignKeyEnum.CONTACT]: getCustomerContactList,
  [FormDesignKeyEnum.CUSTOMER_CONTACT]: getContactListUnderCustomer,
  [FormDesignKeyEnum.CLUE]: getClueList,
  [FormDesignKeyEnum.CLUE_POOL]: getCluePoolList,
  [FormDesignKeyEnum.PRODUCT]: getProductList,
  [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: getOpenSeaCustomerList,
  [FormDesignKeyEnum.BUSINESS_CONTACT]: getOpportunityContactList,
  [FormDesignKeyEnum.CUSTOMER_OPPORTUNITY]: getCustomerOpportunityPage,
  [FormDesignKeyEnum.FOLLOW_RECORD]: getFollowRecordPage,
  [FormDesignKeyEnum.FOLLOW_PLAN]: getFollowPLanPage,
  [FormDesignKeyEnum.CLUE_TRANSITION_CUSTOMER]: getClueTransitionCustomerList,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE]: getAdvancedSearchClueList,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CUSTOMER]: geAdvancedCustomerList,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CONTACT]: getAdvancedCustomerContactList,
  [FormDesignKeyEnum.SEARCH_ADVANCED_PUBLIC]: getAdvancedOpenSeaCustomerList,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE_POOL]: getAdvancedCluePoolList,
  [FormDesignKeyEnum.SEARCH_ADVANCED_OPPORTUNITY]: advancedSearchOptPage,
  [FormDesignKeyEnum.CONTRACT]: getContractList,
  [FormDesignKeyEnum.CONTRACT_PAYMENT]: getPaymentPlanList,
  [FormDesignKeyEnum.CONTRACT_CONTRACT_PAYMENT]: getContractPaymentPlanList,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: getQuotationList,
  [FormDesignKeyEnum.PRICE]: getProductPriceList,
};

export const dataSourceFilterFormKeyMap: Partial<Record<FieldDataSourceTypeEnum, FormDesignKeyEnum>> = {
  [FieldDataSourceTypeEnum.CUSTOMER]: FormDesignKeyEnum.CUSTOMER,
  [FieldDataSourceTypeEnum.BUSINESS]: FormDesignKeyEnum.BUSINESS,
  [FieldDataSourceTypeEnum.CONTACT]: FormDesignKeyEnum.CONTACT,
  [FieldDataSourceTypeEnum.PRODUCT]: FormDesignKeyEnum.PRODUCT,
  [FieldDataSourceTypeEnum.CLUE]: FormDesignKeyEnum.CLUE,
  [FieldDataSourceTypeEnum.PRICE]: FormDesignKeyEnum.PRICE,
  [FieldDataSourceTypeEnum.CONTRACT]: FormDesignKeyEnum.CONTRACT,
  [FieldDataSourceTypeEnum.QUOTATION]: FormDesignKeyEnum.OPPORTUNITY_QUOTATION,
};
